package cn.wildfire.chat.kit.search.module;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.sourcemuch.im.caiba.R;import com.sourcemuch.im.caiba.R2;

import java.util.ArrayList;
import java.util.List;

import cn.wildfire.chat.kit.contact.model.UIUserInfo;
import cn.wildfire.chat.kit.contact.viewholder.UserViewHolder;
import cn.wildfire.chat.kit.conversation.ConversationActivity;
import cn.wildfire.chat.kit.search.SearchableModule;
import cn.wildfirechat.model.Conversation;
import cn.wildfirechat.model.UserInfo;
import cn.wildfirechat.remote.ChatManager;

public class ContactSearchModule extends SearchableModule<UserInfo, UserViewHolder> {
    @Override
    public UserViewHolder onCreateViewHolder(Fragment fragment, @NonNull ViewGroup parent, int viewType) {
        View itemView;
        itemView = LayoutInflater.from(fragment.getActivity()).inflate(R.layout.contact_item_contact, parent, false);
        return new UserViewHolder(fragment, null, itemView);
    }

    @Override
    public void onBind(Fragment fragment, UserViewHolder holder, UserInfo userInfo) {
        holder.onBind(new UIUserInfo(userInfo));
    }

    @Override
    public void onClick(Fragment fragment, UserViewHolder holder, View view, UserInfo userInfo) {
        Intent intent = new Intent(fragment.getActivity(), ConversationActivity.class);
        Conversation conversation = new Conversation(Conversation.ConversationType.Single, userInfo.uid, 0);
        intent.putExtra("conversation", conversation);
        fragment.startActivity(intent);
        fragment.getActivity().finish();
    }

    @Override
    public int getViewType(UserInfo userInfo) {
        return R.layout.contact_item_contact;
    }

    @Override
    public int priority() {
        return 100;
    }

    @Override
    public String category() {
        return "联系人";
    }

    @Override
    public List<UserInfo> search(String keyword) {
        ArrayList<UserInfo> resultList = new ArrayList<>();
        /**
         * modify by HEKE , 2019/8/26 19:12
         * TODO 本地搜索联系人
         */
        try {
            ChatManager chatManager = ChatManager.Instance();
            List<UserInfo> myFriendsList = chatManager.getMyFriendListInfo(true);
            List<UserInfo> searchUserList = chatManager.searchFriends(keyword);
            if (myFriendsList != null && myFriendsList.size() > 0) {
                for(UserInfo userInfo:myFriendsList){
                    if(userInfo.toString().contains(keyword)){
                        resultList.add(userInfo);
                    }
                }
            }
            if (searchUserList != null && searchUserList.size() > 0) {
                for (UserInfo userInfo : searchUserList) {
                    boolean exits = false;
                    for (UserInfo my : myFriendsList) {
                        if (my.uid.equals(userInfo.uid)) {
                            exits = true;
                            break;
                        }
                    }
                    if (!exits) {
                        resultList.add(userInfo);
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultList;
    }
}
